package com.eric.api.ext;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eric.downloader.SimplePageDownloader;
import com.eric.text.AbstractTextWorker;
import com.eric.text.SimpleTextFactory;
import com.eric.text.TextFactory;
import com.eric.text.WorkerType;

public class JuheTopNews {

	String type = "top";

	static String queryBase = "http://v.juhe.cn/toutiao/index?key=559f00c78afbe2e4ca9b57ebeed6eb26&type=";

	public JSONArray fetch() {
		String queryUrl = queryBase + type;
		JSONArray result = new JSONArray();
		SimplePageDownloader spd = new SimplePageDownloader();
		JSONObject json = JSONObject.parseObject(spd.download(queryUrl).text());
		JSONArray ja = json.getJSONObject("result").getJSONArray("data");
		for (int i = 0; i < ja.size(); i++) {
			JSONObject j = new JSONObject();
			String title = ((JSONObject) ja.get(i)).getString("title");
			String date = ((JSONObject) ja.get(i)).getString("date");
			String thumb = ((JSONObject) ja.get(i)).getString("thumbnail_pic_s");
			String url = ((JSONObject) ja.get(i)).getString("url");
			String realtype = ((JSONObject) ja.get(i)).getString("realtype");

			TextFactory tf = new SimpleTextFactory(url);
			AbstractTextWorker tw = tf.createTextWorker(WorkerType.ANSJ_SUMMARY.getName());
			tw.produce();

			j.put("title", title);
			j.put("date", date);
			j.put("thumb", thumb);
			j.put("url", url);
			j.put("realtype", realtype);
			j.put("summary", tw.result().toString().replaceAll("[\\[\\]]", ""));
			result.add(j);
		}
		return result;
	}

}
